#include <STC89C5xRC.H>
#include "delay.h"


unsigned char matrixkey()
{
	unsigned char keynumber=0;
	P1 = 0xFF;
	P13 =0;
	if(P17 == 0){Delayms(20);while(P17 == 0);Delayms(20);keynumber=1;}
	if(P16 == 0){Delayms(20);while(P16 == 0);Delayms(20);keynumber=5;}
	if(P15 == 0){Delayms(20);while(P15 == 0);Delayms(20);keynumber=9;}
	if(P14 == 0){Delayms(20);while(P14 == 0);Delayms(20);keynumber=13;}
	
	P1 = 0xFF;
	P12 =0;
	if(P17 == 0){Delayms(20);while(P17 == 0);Delayms(20);keynumber=2;}
	if(P16 == 0){Delayms(20);while(P16 == 0);Delayms(20);keynumber=6;}
	if(P15 == 0){Delayms(20);while(P15 == 0);Delayms(20);keynumber=10;}
	if(P14 == 0){Delayms(20);while(P14 == 0);Delayms(20);keynumber=14;}
	
	P1 = 0xFF;
	P11 =0;
	if(P17 == 0){Delayms(20);while(P17 == 0);Delayms(20);keynumber=3;}
	if(P16 == 0){Delayms(20);while(P16 == 0);Delayms(20);keynumber=7;}
	if(P15 == 0){Delayms(20);while(P15 == 0);Delayms(20);keynumber=11;}
	if(P14 == 0){Delayms(20);while(P14 == 0);Delayms(20);keynumber=15;}
	
	P1 = 0xFF;
	P10 =0;
	if(P17 == 0){Delayms(20);while(P17 == 0);Delayms(20);keynumber=4;}
	if(P16 == 0){Delayms(20);while(P16 == 0);Delayms(20);keynumber=8;}
	if(P15 == 0){Delayms(20);while(P15 == 0);Delayms(20);keynumber=12;}
	if(P14 == 0){Delayms(20);while(P14 == 0);Delayms(20);keynumber=16;}
	
	return keynumber;
}